from .authFilter import authFilter
from .adminBaseHttp import authHttp
from model import model
import hashlib
import datetime
from utils import ApiBuild

#后台的上传
class blog(authHttp):
    @authFilter.auth
    def get(self):
        self.render("admin/upload/index.html")

    @authFilter.auth
    def post(self, *args, **kwargs):
        try:
            file = self.request.files["file"][0]
            name = hashlib.sha1(file["filename"].encode()).hexdigest()+datetime.datetime.now().strftime("%Y-%m-%d")+file["filename"]
            import os
            file_path = os.path.join("static/upload",name)
            with open(file_path, 'wb') as up:
                up.write(file['body'])
            self.outputJson(ApiBuild.ApiBuild.successWithObject("static/upload/"+name))
        except Exception:
            self.outputJson(ApiBuild.ApiBuild.failWithMessage("上传失败了"))